import {
  Base
} from '../../utils/base.js';
const {
  hotdetail,
  hotiscollect,
  hotcollect
} = require('../../utils/api')
const {
  fetch,
  newFetch,
  hasSensitiveWords,
  replaceAll
} = require('../../utils/fetch')

var base = new Base();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    'id': 0,
    is_collect: true,
    reply_text: '',
    reply_list: [],
    'info': {},
    'content': {},
    'imgs': [],
    'type_id': 0,
    infodetails: '',
    detilid: 0,
    browserList: [],
    specificarea: '100101',
    imglogo: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('specificarea')) {
      this.setData({
        specificarea: wx.getStorageSync('specificarea')
      })
    }
    var id = options.id;

    this.setData({
      "id": id
    })
    var that = this;

    fetch(`wx/message/${this.data.specificarea}/detail`, 'POST', {
      id: id
    }).then(res => {
      if (res) {
        let imgs = [];
        if (res.imgs) {
          imgs = res.imgs.split(",");
        }
        if (res.remark) {
          res.remark_arr = res.remark.split(",");
        }
        this.setData({
          detilid: res.id
        })
        let content = base.jsonToObj(res.content);
        wx.setNavigationBarTitle({
          title: res.typeone.title
        });

        var details = res.details.split('&nbsp;').join('\n')
        that.setData({
          "info": res,
          "content": content,
          "imgs": imgs,
          "type_id": 0,
          infodetails: details
        });

      }
    })

    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      //是否收藏    
      fetch(`wx/message/${this.data.specificarea}/iscollect`, 'POST', {
        messageId: id,
        memberId: userInfo["id"]
      }).then(res => {})

      newFetch(`wx/message/${this.data.specificarea}/saveBrowseLog`, 'GET', {
        messageId: id,
        memberId: userInfo["id"]
        // memberId: 140
      }).then(res => {})

      // //  头像浏览列表
      // newFetch(`wx/message/${this.data.specificarea}/getMemberList`, 'POST', {
      //   messageId:id
      // }).then(res=>{
      //   if(res){
      //     this.setData({
      //       browserList:res
      //     })
      //   }
      // })

    }
    //评论列表
    this.get_reply_list();
  },

  tel: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone
    })
  },
  // 复制内容
  goinputCopy: function (e) {
    var details = e.currentTarget.dataset.details
    wx.setClipboardData({
      data: details,
      success: function (res) {
        // 添加下面的代码可以复写复制成功默认提示文本`内容已复制` 
        wx.showToast({
          title: '复制成功',
          duration: 1500
        })
        wx.getClipboardData({
          success: function (res) {}
        })
      }
    })
  },
  bind_img_view: function (e) {
    let url = base.getDataSet(e, "url");
    wx.previewImage({
      current: url,
      urls: this.data.imgs
    })
  },
  onClickIndex: function (e) {
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  onClickCollect: function (e) {
    var that = this;
    // if(!wx.getStorageSync('userInfo')) {
    //     wx.reLaunch({
    //         url: '/pages/login/index'
    //     })
    //     return;
    // }

    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      fetch(`wx/message/${this.data.specificarea}/collect`, 'POST', {
        messageId: this.data.id,
        memberId: userInfo["id"]
      }).then(res => {
        if (res) {
          that.setData({
            is_collect: false
          });
          base.showmsg('已收藏');
        }
      })
    }
  },

  onChange: function (e) {
    this.data.reply_text = e.detail.value;
  },

  onConfirm() {
    var that = this
    if (this.data.reply_text == '') {
      base.showmsg('请输入评论');
      return;
    }
    wx.showLoading({
        title: '提交中...'
    })
    newFetch(`wx/reply/${this.data.specificarea}/fabu`, 'POST', {
      content: this.data.reply_text,
      memberid: wx.getStorageSync('userInfo').id,
      type: 1,
      typeid:that.data.id,
      area: that.data.specificarea,
    }).then(res => {
      base.showmsg('评论成功');
      wx.hideLoading()
      that.get_reply_list()

      this.setData({
        reply_text: ''
      })
    })

    // wx.request({
    //   url: 'https://api.weixin.qq.com/wxa/msg_sec_checkaccess_token=' + access_token,
    //   method: 'POST',
    //   data: {
    //     content: reply_text
    //   },
    //   success: function (res) {
    //     //当content内含有敏感信息，则返回87014
    //     if (res.data.errcode !== 87014) {
    //       // 合格
         
    //     } else {
    //       wx.showToast({
    //         title: '输入内容包含敏感词,请重新输入',
    //         icon: 'none',
    //         duration: 1500
    //       })
    //     }
    //   }
    // })
  },

  get_reply_list() {
    var that = this;
    var param = {
      url: `reply/${that.data.specificarea}/list`,
      data: {
        // "area": this.data.specificarea,
        "type": 1,
        "typeid": this.data.id
      },
      sCallback: function (res) {
        if (!res) {
          res = {
            list: []
          }
        }
        that.setData({
          reply_list: res.list,
        });
      }
    };
    base.request(param);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      //是否收藏    
      fetch(`wx/membershare/${this.data.specificarea}/addShare`, 'POST', {
        type: 1,
        typeId: this.data.detilid,
        memberId: userInfo["id"]
      }).then(res => {
        if (res) {}
      })
    }
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: this.data.info.title, // 默认是小程序的名称(可以写slogan等)
      path: '/pages/indexDetails/index?id=' + this.data.id, // 默认是当前页面，必须是以‘/’开头的完整路径
      imageUrl: this.data.info.logo || '/resources/imgs/agent.png', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {

        }
      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          // 用户取消转发
        } else if (res.errMsg == 'shareAppMessage:fail') {
          // 转发失败，其中 detail message 为详细失败信息
        }
      },
      complete: function (com) {}
    }
    // 返回shareObj
    return shareObj;
  },
  //用户点击右上角分享朋友圈
  // https://www.xzwbfuwu.cn/images/icon/index/erji/dating.jpg
  // http://www.xzwbfuwu.cn/upload/images/20201026/a747116e3b9347fea7cba75c1d57085e.jpg
  // https://www.xzwbfuwu.cn/upload/images/20201026/a747116e3b9347fea7cba75c1d57085e.jpg

  onShareTimeline: function () {
    var that = this;
    var imglogo = this.data.info.logo || '/resources/imgs/agent.png'
    var imgUrl = this.data.info.logo.indexOf('http://www.xzwbfuwu.cn')
    if (imgUrl == 0) {
      var gologos = imglogo.replace('http://www.xzwbfuwu.cn', 'https://www.xzwbfuwu.cn')
      imglogo = gologos
    } else {
      imglogo = this.data.info.logo
    }
    return {
      title: that.data.info.title,
      path: '/pages/indexDetails/index?id=' + that.data.id, // 默认是当前页面，必须是以‘/’开头的完整路径
      // imageUrl: '/resources/imgs/agent.png',
      imageUrl: imglogo || '/resources/imgs/agent.png',
      query: {
        key: ''
      },
    }
  }
})